package com.example.demomk;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demomk.Adapter.DetailsAdapter;
import com.example.demomk.Utils.CustomNavBar;
import com.example.demomk.Utils.baseActivity;
import com.example.demomk.ViewModel.MeasurementViewModel;
import com.example.demomk.databinding.ActivityDetailsBinding;

public class DetailsActivity extends baseActivity {
    private DetailsAdapter adapter;
    private RecyclerView recyclerView;
    private MeasurementViewModel viewmodel;
    private ActivityDetailsBinding binding;
    private CustomNavBar customNavBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Intent  intent = getIntent();
        int demo = intent.getIntExtra("measurementId",0);

        //顶部导航栏

        recyclerView = binding.detailRecycler;
        adapter = new DetailsAdapter();

        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));



        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.custom_divider));
        recyclerView.addItemDecoration(dividerItemDecoration);



        TextView dataText = findViewById(R.id.dataText);
        TextView auditText = findViewById(R.id.auditText);
        dataText.setSelected(true);
        dataText.setOnClickListener(v -> {
            if (!dataText.isSelected()) {
                dataText.setSelected(true);
                auditText.setSelected(false);
            }
        });
        auditText.setOnClickListener(v -> {
            if (!auditText.isSelected()) {
                auditText.setSelected(true);
                dataText.setSelected(false);
            }
        });



        viewmodel = new ViewModelProvider(this).get(MeasurementViewModel.class);

        viewmodel.getAllAvgMeasurementId(demo).observe(this, avgMeasurementWithDetails -> {
//            binding.name.setText(avgMeasurementWithDetails.getUser_name());
            binding.measurementname.setText(avgMeasurementWithDetails.getUser_name());
            String time = avgMeasurementWithDetails.getStart_time().split(" ")[0] ;
            String times = time +"\n" + time;
            binding.measurementtime.setText(times);
            binding.location.setText(avgMeasurementWithDetails.getLocation_name());
            binding.group.setText(avgMeasurementWithDetails.getGroup_name());
            binding.flow.setText(String.valueOf(avgMeasurementWithDetails.getFlow()) + "LPM");
        });


        viewmodel.getAvgMeasurementWithDetails(demo).observe(this, avgMeasurementWithMeasurements -> {
            Log.d("AvgMeasurementWithMeasurements", avgMeasurementWithMeasurements.measurements.size() + "");
            if (avgMeasurementWithMeasurements != null) {
                adapter.setMeasurements(avgMeasurementWithMeasurements.measurements);
            }
        });

        binding.icon.setOnClickListener(v -> {
            finish();
        });

        viewmodel.getMeasurementCount().observe(this, count -> {
            if (count != null) {
                String countString = String.valueOf(count);
                binding.count.setText("样本数: "+ countString);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(customNavBar != null){
            customNavBar.destroy();
        }
    }
}